    //
//  PhotoFrameVideoPlayViewController.m
//  PhotoFrame
//
//  Created by William on 11-5-11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <MediaPlayer/MediaPlayer.h>
#import "PhotoFrameVideoPlayViewController.h"

@implementation PhotoFrameVideoPlayViewController
@synthesize delegate;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [self.view setAutoresizesSubviews:NO];
    [super viewDidLoad];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


#pragma mark -
#pragma mark Play Movie Source Methods
- (void)playMovie:(NSString*)path {
    NSURL *theURL = [NSURL URLWithString:path];
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
    [player.view setFrame:[delegate getCurrentRect]];
    [player.view setCenter:[delegate getCurrentCenter]];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(finishPreload:)
												 name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification
											   object:player];
    [[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(finishPlayback:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:player];
    [self.view setHidden:YES];
}

- (void)finishPreload:(NSNotification *)aNotification {
    MPMoviePlayerController *player = [aNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification
                                                  object:player];
	[delegate pauseTimer];
    [self.view setHidden:NO];
	[self.view addSubview:player.view];
    [player play];
}

- (void)finishPlayback:(NSNotification *)aNotification {
    MPMoviePlayerController *player = [aNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:player];
    [player stop];
	[self.view sendSubviewToBack:player.view];
    [player release];
    player = nil;
    [self.view setHidden:YES];
	[delegate resumeTimer];
}
@end
